#include <fstream>
using namespace std;
#include "axes.h"
#include <string.h>

#include "ndim-utils.h"

struct __attribute__ ((__packed__)) spec_tic_t
{
  spec_tic_t(int x, int y) { this->x = x; this->y = y; }
  int x, y;
  static void register_dyntype()
  {
    CompoundDynType* dt = new CompoundDynType("spec_tic_t");
    dt->add_member("x", "int", offset(&spec_tic_t::x), 1);
    dt->add_member("y", "int", offset(&spec_tic_t::x), 1);
    DynTypeManager::get()->register_dyntype(dt);
  }
};

template <> DynType* get_dyntype<spec_tic_t>() {
  return DynTypeManager::get()->get_dyntype("spec_tic_t");
}

int main()
{
  try {
    spec_tic_t::register_dyntype();
    DynTypeManager::get()->get_dyntype("spec_tic_t")->print(cout); cout << endl;
    
    Axes axes;
    axes.add_axis<int>("X");
    axes.add_axis<CharN<6> >("Y");
    axes.add_axis<spec_tic_t>("Z");
    
    axes.get("X")->add_tic<int>(1, 0);
    axes.get("X")->add_tic<int>(1, 11);
    axes.get("Y")->add_tic<CharN<6> >(CharN<6>("hi"), 0);
    axes.get("Y")->add_tic<CharN<6> >(CharN<6>("hi"), 0);
    axes.get("Z")->add_tic<spec_tic_t>(spec_tic_t(1, 2), 0);
    
    axes.get_at(0)->add_tic<int>(2, 0);
    axes.get_at(1)->add_tic<CharN<6> >(CharN<6>("bye"), 0);
    axes.get_at(2)->add_tic<spec_tic_t>(spec_tic_t(2, 2), 0);
    
    for (int i = 500; i < 100; i++) {
      axes.get_at(0)->add_tic<int>(i, i);
    }
    
    axes.print(cout);
    ofstream out("AXES.DAT");
    axes.save(out);
    
    AxesCoor coor(&axes);
    coor.set<int>("X", 1);
    coor.set<CharN<6> >("Y", CharN<6>("hi"));
    coor.set<spec_tic_t>("Z", spec_tic_t(2,2));

    axes.get_at(1)->print(cout); cout << endl;
    
    vector<axis_index_t> axis_coor = axes.get_axis_indexes_coor(coor);
    print_coor(cout, axis_coor); cout << endl;
  } catch (Error& e) {
    cerr << "Error caught: " << e.msg() << endl;
  }
}

